<template>
	<view class="loginPage">
		<image class="logo" src="/static/image/logo.png"></image>
		<text class="proName">美试客</text>
		<view class="sBtn" @click="getUserInfo">微信用户快捷方式登录</view>
		<view class="xieyi" @click="read">
			<image :src="ifread?'/static/image/icon/icon-checkbox-checked-a.png':'/static/image/icon/icon-uncheck.png'"></image>
			<text style="color: #666;">阅读并同意</text>
			<text style="color: #ff4544;" @click.stop="toxieyi">《用户服务协议及隐私政策》</text>
		</view>
	</view>
</template>

<script>
	import {
		post,getUrlKey,get
	} from "@/utils/post.js";
	export default {
		data() {
			return {
				iflogin:false,
				ifread:false,
				comType:'',
				needmobile:false
			}
		},
		onLoad(op) {
			this.comType = getUrlKey('comType') || ''
			this.code = getUrlKey('code')
			if(this.code){
				this.iflogin = true
				this.login()
				return
			}
		},
		onShow(){
			if(uni.getStorageSync('_USER_ACCESS_TOKEN')){
				uni.switchTab({
					url: '/pages/user-center/user-center'
				})
			}
		},
		methods: {
			getUserInfo(){
				if(!this.ifread){
					this.$showM('请阅读并同意《用户服务协议及隐私政策》',false)
					return
				}
				this.getCode()
			},
			getCode() {
				var test2 = window.location.href;
				var url = encodeURIComponent(test2)
				var wxRedirectUri =
					'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa0b12b81b99bad7a&redirect_uri=' + url +
					'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
				window.location = wxRedirectUri
			},
			login(){
				var data = {
					code:this.code
				}
				// const resolve = this.$user.getUserInfoResolve;
				// const reject = this.$user.getUserInfoReject;
				post(this.$api.passport.login,data,true,res=>{
					//uni.setStorageSync('invite_id',res.data.invite_code)
					uni.setStorageSync('_USER_ACCESS_TOKEN',res.data.access_token)
					uni.setStorageSync('uid',res.data.user_id)
					setTimeout(()=>{
						this.toindex()
					},50)
				},fail=>{
					return
					this.login()
				})
			},
			toindex(){
				//通过缓存  决定跳转  
				var sharegoods = uni.getStorageSync('sharegoods')
				var gtype = uni.getStorageSync('gtype')
				var sharepage = uni.getStorageSync('sharepage')
				var s_mch_id = uni.getStorageSync('s_mch_id')
				if(sharegoods){
					if(gtype){
						uni.reLaunch({
							url:`/plugins/jimai/goods?type=${gtype}&goods_id=${sharegoods}`
						})
					}else if(sharepage){
						uni.reLaunch({
							url:`/${sharepage}?mch_id=${s_mch_id}&goods_id=${sharegoods}`
						})
					}
				}else{
					uni.reLaunch({
						url: '/pages/user-center/user-center'
					})
				}
				
			},
			read(){
				this.ifread = !this.ifread
			},
			toxieyi(){
				uni.navigateTo({
					url:'/pages/login/xieyi'
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.sBtn {
		background: #08c261;
		color: #fff;
		width: 600upx;
		font-size: 30upx;
		height: 70upx;
		line-height: 70upx;
		margin-top: 250upx;
		text-align: center;
		border-radius: 10upx;
	}

	.proName {
		font-size: 30upx;
		font-weight: bold;
		margin-top: 10upx;
	}
		
	.logo {
		width: 300upx;
		height: 300upx;
		border-radius: 20upx;
		margin-top: 150upx;
	}
	
	.xieyi {
		margin-top: 50upx;
		width: 100%;
		font-size: 26upx;
		display: flex;
		align-items: center;
		margin-left: 150upx;
	}
	.xieyi image{
		width: 40upx;
		height: 40upx;
		margin-right: 20upx;
	}

	.loginPage {
		width: 750upx;
		height: 100vh;
		background: #fff;
		display: flex;
		align-items: center;
		flex-direction: column;
	}
</style>
